


//
//  ESTableHeaderView.m
//  Eyeshot
//
//  Created by admin on 15/7/4.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESTableHeaderView.h"

@interface ESTableHeaderView ()

@property (nonatomic, weak) UILabel *levelLabel;
@property (nonatomic, weak) UILabel *scoreLabel;
@property (nonatomic, weak) UIImageView *imageV;//横线
@property (nonatomic, weak) UILabel *introductionLabel;//文字描述lable
@end

@implementation ESTableHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.headPhotoBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:self.headPhotoBtn];
        
        self.nameBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview: self.nameBtn];
        
  
        UILabel *levelLabel = [[UILabel alloc] init];
        self.levelLabel = levelLabel;
        [self addSubview:levelLabel];
        
       
        UILabel *scoreLabel = [[UILabel alloc] init];
        self.scoreLabel = scoreLabel;
//        scoreLabel.backgroundColor = [UIColor orangeColor];
        [self addSubview:scoreLabel];
        
        UIImageView *imageV = [[UIImageView alloc] init];
        self.imageV = imageV;
        [self addSubview:imageV];
 
        UILabel *introLabel = [[UILabel alloc] init];
        self.introductionLabel = introLabel;
        self.introductionLabel.font = [UIFont systemFontOfSize:12];
//        self.introductionLabel.backgroundColor = [UIColor redColor];
        [self addSubview:introLabel];

        self.cardBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        self.cardBtn.backgroundColor = [UIColor orangeColor];
        [self addSubview:self.cardBtn];
        
        
        self.commentBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:self.commentBtn];
        
        self.attentionDetailBtn = [ESUpAndDownButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:self.attentionDetailBtn];
        
        self.fansBtn = [ESUpAndDownButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:self.fansBtn];
        
        
        self.attentionBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:self.attentionBtn];
        
     
        self.likeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:self.likeBtn];

    }
    return self;
}

- (void)setProfileModel:(ESPhotoProfileModel *)profileModel
{
    _profileModel = profileModel;
    
    //设置frame
    CGFloat headPhotoBtnX = kHorizen * 4;
    CGFloat headPhotoBtnY = -kHorizen * 5;
    CGFloat headPhotoBtnW = kHorizen * 10;
    CGFloat headPhotoBtnH = kHorizen * 10;
    self.headPhotoBtn.frame = CGRectMake(headPhotoBtnX, headPhotoBtnY, headPhotoBtnW, headPhotoBtnH);
//    self.headPhotoBtn.backgroundColor = [UIColor greenColor];
    self.headPhotoBtn.layer.cornerRadius = headPhotoBtnW/2;
    self.headPhotoBtn.layer.masksToBounds = YES;
    
    CGFloat nameBtnX = CGRectGetMaxX(self.headPhotoBtn.frame) + 10;
    CGFloat nameBtnY =  - kHorizen * 4;
    CGFloat nameBtnW = kScreenW - nameBtnX * 2;
    CGFloat nameBtnH = kVertical * 3.5;
    self.nameBtn.frame = CGRectMake(nameBtnX,nameBtnY, nameBtnW, nameBtnH);
//    self.nameBtn.backgroundColor = [UIColor yellowColor];
    
    CGFloat levelLabelX = nameBtnX;
    CGFloat levelLabelY = kVertical;
    CGFloat levelLabelW = kHorizen * 15;
    CGFloat levelLabelH = kVertical * 3.5;
    self.levelLabel.frame = CGRectMake(levelLabelX, levelLabelY, levelLabelW, levelLabelH);
//    self.levelLabel.backgroundColor = [UIColor orangeColor];
    
    CGFloat scoreLabelX = nameBtnX;
    CGFloat scoreLabelY = CGRectGetMaxY(self.levelLabel.frame) + kVertical;
    CGFloat scoreLabelW = kHorizen * 15;
    CGFloat scoreLabelH = kVertical * 3.5;
    self.scoreLabel.frame = CGRectMake(scoreLabelX, scoreLabelY, scoreLabelW, scoreLabelH);
    
    CGFloat imageVY = CGRectGetMaxY(self.scoreLabel.frame) + kVertical * 2;
    [self.imageV setImage:[UIImage imageNamed:@"cell-content-line"]];
    self.imageV.frame = CGRectMake(0, imageVY, kScreenW, 0.5);
    
     CGFloat introductionLabelX = 0;
     CGFloat introductionLabelY = imageVY;
     CGFloat introductionLabelW = kScreenW;
    CGFloat introductionLabelH = 0;
    
    if ([profileModel.introduction isEqualToString:@""])
    {
        self.introductionLabel.frame = CGRectMake(introductionLabelX, introductionLabelY, 0, introductionLabelH);
    }else
    {
        introductionLabelH = [ESTableHeaderView heightForStr:profileModel.introduction fontSize:12];
        self.introductionLabel.frame = CGRectMake(introductionLabelX,introductionLabelY , introductionLabelW, introductionLabelH);
        
        self.introductionLabel.text = self.profileModel.introduction;
        
    }
   
    CGFloat cardBtnY = introductionLabelY +  introductionLabelH + kVertical;
    CGFloat cardBtnH = kVertical * 4;
    self.cardBtn.frame = CGRectMake(0, cardBtnY, kScreenW/2, cardBtnH);
    [self.cardBtn setTitle:@"帖子" forState:(UIControlStateNormal)];
    self.cardBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.cardBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.cardBtn setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)];
    
    self.commentBtn.frame = CGRectMake(kScreenW/2, cardBtnY, kScreenW/2, cardBtnH);
    [self.commentBtn setTitle:@"评论" forState:(UIControlStateNormal)];
    self.commentBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.commentBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.commentBtn setTitleColor:[UIColor redColor] forState:(UIControlStateSelected)];
    
    self.attentionDetailBtn.frame = CGRectMake(kScreenW - 110, 5, 40, imageVY - 10);
    self.attentionDetailBtn.downLable.text = @"关注";
    self.attentionDetailBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    self.attentionDetailBtn.downLable.font = [UIFont systemFontOfSize:12];
    [self.attentionDetailBtn setContentColor:[UIColor blackColor]];

    self.fansBtn.frame = CGRectMake(kScreenW - 60, 5, 40, imageVY - 10);
    self.fansBtn.downLable.text = @"粉丝";
    self.fansBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    self.fansBtn.downLable.font = [UIFont systemFontOfSize:12];
    [self.fansBtn setContentColor:[UIColor blackColor]];

    self.attentionBtn.frame = CGRectMake(kScreenW - 60, -60, 50, 20);
    [self.attentionBtn setBackgroundImage:[UIImage imageNamed:@"Profile_followBtnBg"] forState:(UIControlStateNormal)];
    [self.attentionBtn setTitle:@"关注" forState:(UIControlStateNormal)];
    self.attentionBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    self.attentionBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 15, 3, 3);
    [self.attentionBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];

    self.likeBtn.frame = CGRectMake(kScreenW - 60, - 120, 50, 20);
    [self.likeBtn setBackgroundImage:[UIImage imageNamed:@"commentNewButtonFrameClick"] forState:(UIControlStateNormal)];
    [self.likeBtn setImage:[UIImage imageNamed:@"commentLikeButton"] forState:(UIControlStateNormal)];
    self.likeBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 3, 3, 3);
   
    
    [self.headPhotoBtn sd_setBackgroundImageWithURL:[NSURL URLWithString: profileModel.profile_image] forState:(UIControlStateNormal)];
    if ([profileModel.sex isEqualToString:@"f"])
    {
        [self.nameBtn setImage:[UIImage imageNamed:@"Profile_womanIcon"] forState:(UIControlStateNormal)];
        
    }else
    {
        [self.nameBtn setImage:[UIImage imageNamed:@"Profile_manIcon"] forState:(UIControlStateNormal)];

    }
    
    [self.nameBtn setTitle:profileModel.username forState:(UIControlStateNormal)];
    [self.nameBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.nameBtn.titleLabel.font = [UIFont systemFontOfSize:12];

    
    [self.likeBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    if ([profileModel.praise_count isEqualToString:@""])
    {
        [self.likeBtn setTitle:@"0" forState:(UIControlStateNormal)];
    }else{
         [self.likeBtn setTitle:profileModel.praise_count forState:(UIControlStateNormal)];
    }
    self.likeBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    
    if ([profileModel.follow_count isEqualToString:@""])
    {
        [self.attentionDetailBtn setTitle:@"0" forState:(UIControlStateNormal)];
    }else{
        [self.attentionDetailBtn setTitle:profileModel.follow_count forState:(UIControlStateNormal)];
        
    }
    
    if ([profileModel.fans_count isEqualToString:@""])
    {
        self.fansBtn.titleLabel.text = @"0";

    }else{
        [self.fansBtn setTitle:profileModel.fans_count forState:(UIControlStateNormal)];
    }
    
    self.levelLabel.text = [NSString stringWithFormat:@"等级:LV%@",profileModel.level];
    self.levelLabel.font = [UIFont systemFontOfSize:12];
    self.scoreLabel.text = [NSString stringWithFormat:@"积分:%@",profileModel.credit];
    self.scoreLabel.font = [UIFont systemFontOfSize:12];
  
    
}

+ (CGFloat)heightForStr:(NSString *)str fontSize:(NSInteger)size
{
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size] forKey:NSFontAttributeName];
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(kScreenW, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    return bounds.size.height;
}

+ (CGFloat)heightForHeaderViewWithModel:(ESPhotoProfileModel *)model
{
    CGFloat introduceH = [self heightForStr:model.introduction fontSize:12];
    return kVertical * 12 + introduceH + kVertical * 5;
}

@end
